import 'package:flutter/material.dart';
import 'package:hive_flutter/adapters.dart';
import 'package:ichinesereader/constants.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/register.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';

class LevelPlacementChioceChipWidget extends StatefulWidget {
  LevelPlacementChioceChipWidget({Key? key}) : super(key: key);

  @override
  State<LevelPlacementChioceChipWidget> createState() =>
      _LevelPlacementChioceChipWidgetState();
}

class _LevelPlacementChioceChipWidgetState
    extends State<LevelPlacementChioceChipWidget> {
  var box = Hive.box('appconfig');
  var levelOptions = Constants.levelOptions;
  int selectedLevelOption = 0;
  RegisterController _registerController = Get.find();

  @override
  Widget build(BuildContext context) {
    Log.debug('build LevelPlacementChioceChipWidget');
    levelOptions.removeWhere((element) {
      return element.value.toString() == 'pre';
    });
    levelOptions.removeWhere((element) {
      return element.value.toString() == 'level0';
    });

    return Container(
      height: 45.0,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: Constants.levelOptions.length,
        itemBuilder: ((context, index) {
          return Padding(
            padding: const EdgeInsets.symmetric(
              horizontal: 3.0,
            ),
            child: ChoiceChip(
              padding: EdgeInsets.symmetric(horizontal: 8.0),
              shape: BeveledRectangleBorder(
                side: BorderSide(color: Colors.white, width: 0.3),
                borderRadius: BorderRadius.circular(1.0),
              ),
              backgroundColor: hexStringToHexInt(
                Constants.levelOptions[index].color.toString(),
              ),
              label: Text(
                Constants.levelOptions[index].label
                    .toString()
                    .replaceAll('Level 0', 'N/A')
                    .replaceAll('Level ', ''),
                style:
                    TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
              ),
              selectedColor: Colors.black,
              selected: selectedLevelOption == index,
              onSelected: (selected) {
                setState(() {
                  selectedLevelOption = selected ? index : 0;
                  _registerController.setLevelText(selectedLevelOption);
                });
              },
            ),
          );
        }),
      ),
    );
  }
}
